﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TextManager.Models;

namespace TextManager.Controllers
{
    public class TaskController : Controller
    {
        Model1 model1 = new Model1();
        // GET: Task
        public ActionResult Index(int id = 0, string Content = "")
        {
            //获取所有用户列表
            var users = model1.Project.ToList();
            //在用户列表开始位置添加全部选项
            users.Insert(0, new Project() { ProjectName = "全部", ProjectID = 0 });
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(users, "ProjectID", "ProjectName");
            //根据条件查询数据
            var list = model1.Task.Where(p => p.Content.Contains(Content) && (id == 0 || p.ProjectID == id)).ToList();
            return View(list);
        }

        public ActionResult Create()
        {
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(model1.Project, "ProjectID", "ProjectName");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Task task)
        {
            if (ModelState.IsValid)
            {
                //与用户交互的属性，直接使用传递的数据
                //特殊处理：未与用户交互，且默认值无法满足数据库要求的属性必须重新赋值（数据库默认值）
                task.AddTime = DateTime.Now;
                task.TaskState = 1;
                task.FinishedTime = null;
                model1.Task.Add(task);
                var res = model1.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            ViewBag.id = new SelectList(model1.Project, "ProjectID", "ProjectName");
            return View(task);
        }

        public ActionResult Edit(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var task = model1.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return RedirectToAction("Index");
            }
            //生成下拉框数据，属性名与实体属性保持一致
            ViewBag.id = new SelectList(model1.Project, "ProjectID", "ProjectName", task.ProjectID);
            return View(task);
        }
        [HttpPost]
        public ActionResult Edit(Task task)
        {
            if (ModelState.IsValid)
            {
                var temp = model1.Task.FirstOrDefault(p => p.TaskID == task.TaskID);
                if (temp == null)
                {
                    return RedirectToAction("Index");
                }
                temp.Content = task.Content;
                temp.FinishedTime = task.FinishedTime;
                temp.TaskPriority = task.TaskPriority;
                temp.TaskState = task.TaskState;
                temp.ProjectID = task.ProjectID;
                var res = model1.SaveChanges();
                if (res > 0)
                {
                    return RedirectToAction("Index");
                }
            }
            return View(task);
        }

        public ActionResult Details(int? id)
        {
            if (id == null)
            {
                return RedirectToAction("Index");
            }
            var task = model1.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return RedirectToAction("Index");
            }
            return View(task);
        }
        [HttpPost]
        public ActionResult Delete(int? id)
        {
            if (id == null)
            {
                return Json(false);
            }
            var task = model1.Task.FirstOrDefault(p => p.TaskID == id);
            if (task == null)
            {
                return Json(false);
            }
            model1.Task.Remove(task);
            var res = model1.SaveChanges();
            return Json(res > 0);
        }
    }
}